package com.xiaozhou.file;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
public class JSONUtils {


    public static  Map<String, String> getProductMap(){
        // 方法2: 使用 Jackson 库
        System.out.println("\n使用 Jackson 库读取 JSON:");
        ObjectMapper mapper = new ObjectMapper();
        try {
            List<ProductDTO> list = mapper.readValue(new FileReader("C:\\Users\\Administrator\\Desktop\\产品1.json"), new TypeReference<List<ProductDTO>>() {
            });
            Map<String, String> collect = list.stream().collect(Collectors.toMap(ProductDTO::getProductCode, ProductDTO::getProductFullName));
            return collect;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Map<String, String> getCompanyMap(){
        // 方法2: 使用 Jackson 库
        System.out.println("\n使用 Jackson 库读取 JSON:");
        ObjectMapper mapper = new ObjectMapper();
        try {
            List<CompanyDTO> list = mapper.readValue(new FileReader("C:\\Users\\Administrator\\Desktop\\保司1.json"), new TypeReference<List<CompanyDTO>>() {
            });
            Map<String, String> collect = list.stream().collect(Collectors.toMap(CompanyDTO::getCompanyCode, CompanyDTO::getCompanyFullName));
            return collect;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
    }
}
